package cn.sumpu.app.guoguojie.dao.impl;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;
import java.util.Map;

import cn.sumpu.app.guoguojie.dao.DoorplateDao;
import cn.sumpu.app.guoguojie.domain.Doorplate;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class DoorplateDaoImpl extends SqlSessionDaoSupport implements
		DoorplateDao {

	@SuppressWarnings("unchecked")
	public List<Doorplate> getDoorplateList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DoorplateMapper.select",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public int getListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.DoorplateMapper.getListCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getListCount failure.", e);
			}
			throw new AppBusinessException("getListCount failure.", e);
		}
	}

	@Override
	public void insert(Doorplate doorplate) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.DoorplateMapper.insertDoorplate",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Insert database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public void update(Doorplate doorplate) throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.DoorplateMapper.upadteDoorplate",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Update database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}

	}

	public Doorplate getVipDoorplateList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Doorplate) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.DoorplateMapper.vipSelectDoorplate",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("QueryVipdoorplate database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

}
